
#include "ProductsParser.h"
#include "Helper.h"

#include <assert.h>
#include <QRegExp>
#include <QDir>
#include <QDebug>

using namespace ecocity;

//---------------------------------------------------------

bool
ProductsParser::parse( const QString& file_products_csv,
                       std::vector< Product >& products,
                       std::vector< Proveider >& proveiders )
{
    products.clear( );

    // expects '/' as directory separator regardless of operating system.
    QFile file_products( file_products_csv );

    bool result = true;

    if ( !file_products.open( QFile::ReadOnly ) )
    {
        assert( 0 );
        result = false;
    }

    if( !parse_file( file_products, products, proveiders ) )
    {
        assert( 0 );
        result = false;
    }

    file_products.close( );

    return result;
}

//---------------------------------------------------------

bool
ProductsParser::parse_file( QFile& file,
                            std::vector< Product >& products,
                            std::vector< Proveider >& proveiders,
                            const char separator )
{
    QTextStream stream( &file );

    while ( !stream.atEnd( ) )
    {
        QString line = stream.readLine( );

        while( line.startsWith( '#' ) )
        {
            line = stream.readLine( );
        }

        std::vector<int> separators =
                separators_in_line( line, separator );

        // list should contain 9 separators
        //      id,Name,Description,Provider,TaxRate,PriceCoope,PriceShop,PriceCustomer,Price
        // i.e. 1,Pan rustic,Pan rustic eco,La Fogaina, 8%, 2EUR+30%, 2EUR+25%, 2EUR+20%, 2EUR
        if( separators.size() < ProductsModel::s_columns -1 )
        {
            assert( 0 );
            return false;
        }

        parse_line( line, products, separator );

        proveiders = Helper::get_proveiders( products );
    }

    return true;
}

//---------------------------------------------------------

void
ProductsParser::parse_line( const QString &line,
                            std::vector< Product>& products,
                            const char separator )
{
    Product product = to_product( line, separator );

    products.push_back( product );
}

//---------------------------------------------------------

Product
ProductsParser::to_product( const QString &line, const char separator )
{
    int product_id = line.section( separator,
                                   PRODUCT_ID,
                                   PRODUCT_ID ).toInt( );

    QString product_name = line.section( separator,
                                         PRODUCT_NAME,
                                         PRODUCT_NAME );

    QString product_description = line.section( separator,
                                                PRODUCT_DESCRIPTION,
                                                PRODUCT_DESCRIPTION );

    QString product_price_type = line.section( separator,
                                               PRODUCT_PRICE_TYPE,
                                               PRODUCT_PRICE_TYPE );

    Proveider product_provider = to_proveider( line, separator );

    float product_price = line.section( separator,
                                        PRODUCT_COST_ECOCITY,
                                        PRODUCT_COST_ECOCITY ).toFloat( );

    uint product_tax_rate = line.section( separator,
                                          PRODUCT_TAX_RATE,
                                          PRODUCT_TAX_RATE ).toUInt();

    float profit_rate = line.section( separator,
                                      PRODUCT_PROFIT_RATE,
                                      PRODUCT_PROFIT_RATE ).toFloat();


    Product product( product_id,             // product_id
                     product_name,           // product_name
                     product_description,    // product_description
                     product_provider,       // product_provider
                     product_price,          // price without tax and profit
                     Product::to_price_type( product_price_type ),
                     product_tax_rate,       // product_tax_rate
                     profit_rate );          // product profit rate


    return product;
}

//---------------------------------------------------------

Proveider
ProductsParser::to_proveider( const QString &line, const char separator )
{
    const QString& proveider_name =
            line.section( separator, PRODUCT_PROVIDER, PRODUCT_PROVIDER );

    Proveider proveider( proveider_name );

    return proveider;
}

//---------------------------------------------------------

std::vector<int>
ProductsParser::separators_in_line( const QString& line, const char separator )
{
    std::vector<int> separators;

    int index = line.indexOf( separator, 0 );

    while ( index != -1 )
    {
        separators.push_back( index );
        index = line.indexOf( separator, index+1 );
    }

    return separators;
}

//---------------------------------------------------------
